#!/bin/bash
exec_name=EttaM1
package_dir=`dirname $(pwd)`
package_name=`basename $package_dir`
current_user=`whoami`
install_dir=/home/$current_user/$exec_name
dist_dir=$install_dir/dist
database_dir=$install_dir/data
logo_file=etta-m1.png
datapath_file=datapath.txt
new_install=0
exit_code=0

check_version() {
  if [ -L $install_dir/$exec_name ]; then
     local install_version_dir=`dirname $(readlink $install_dir/$exec_name)`
     local install_version=`basename $install_version_dir`
     local current_version=$package_name
     if [ x"$current_version" = x"$install_version" ]; then
       echo -e "\033[1;31merror:\033[0m" "'$current_version' is installed."
       exit 255
     fi
  fi
}

make_install_dir() {
if [ ! -d $install_dir ]; then new_install=1; fi
mkdir -p $dist_dir/ >/dev/null 2>&1
mkdir -p $database_dir/ >/dev/null 2>&1
}

copy_package_file() {
local src_file=$package_dir/$1
local dst_file=$install_dir/$1
local attr_hide="norm"
local attr_copy="skip"

for i in "$@"; do
  [ "${i}" == "hide" ]     && { attr_hide="hide"; }
  [ "${i}" == "override" ] && { attr_copy="override"; }
done

[ x"hide" == x"$attr_hide" ] && { dst_file=$install_dir/.$1; }

if [ x"override" == x"$attr_copy" ] || [ ! -f $dst_file ]; then
  cp $src_file $dst_file || { exit_code=1; }
else
  echo "skip copy file: $src_file -> $install_dir/"
fi
}

copy_package_dir() {
if [ ! -d $dist_dir/`basename $1` ]; then
  cp -r $1/ $dist_dir/
  if [ $? -ne 0 ]; then exit_code=2; fi
else
  echo "skip copy directory: $1 -> $dist_dir/"
fi
}

copy_package_data() {
copy_package_dir $package_dir
copy_package_file $logo_file "hide"
}

create_exec_link_file() {
rm -f $install_dir/$exec_name >/dev/null 2>&1
ln -s $dist_dir/$package_name/$exec_name $install_dir/$exec_name
if [ $? -ne 0 ]; then exit_code=3; fi
}

redirect_data_dir() {
echo $install_dir > $dist_dir/$package_name/$datapath_file
if [ $? -ne 0 ]; then exit_code=4; fi
}

make_desktop_file() {
echo "[Desktop Entry]"
echo "Name=$exec_name"
echo "Terminal=false"
echo "Type=Application"
echo "Categories=Application;"
echo "Comment=M1 Equipment Software."
echo "Exec=$install_dir/$exec_name"
echo "Icon=$install_dir/.$logo_file"
}

enable_desktop_icon() {
local user_desktop_dir=/home/$current_user/Desktop
rm -f $user_desktop_dir/$exec_name*.desktop >/dev/null 2>&1
make_desktop_file > $user_desktop_dir/$exec_name.desktop
gio set $user_desktop_dir/$exec_name.desktop metadata::trusted true
chmod +x $user_desktop_dir/$exec_name.desktop
[ -z "$(gio info $user_desktop_dir/$exec_name.desktop | grep "metadata::trusted")" ] && rm -f $user_desktop_dir/$exec_name.desktop
}

enable_dock_icon() {
local user_desktop_dock_dir=/home/$current_user/.local/share/applications
rm -f $user_desktop_dock_dir/$exec_name*.desktop >/dev/null 2>&1
make_desktop_file > $user_desktop_dock_dir/$exec_name.desktop
}

enable_user_autostart() {
local user_autostart_dir=/home/$current_user/.config/autostart
mkdir -p $user_autostart_dir/ >/dev/null 2>&1
rm -f $user_autostart_dir/$exec_name*.desktop >/dev/null 2>&1
make_desktop_file > $user_autostart_dir/$exec_name.desktop
}

backup_last_version() {
local backup_error_code=0

if [ -L $install_dir/$exec_name ]; then
  local last_version_dir_path=`dirname $(readlink $install_dir/$exec_name)`
  local last_version_dir_name=`basename $last_version_dir_path`
  
  cd $last_version_dir_path/../

  tar -zcvf $last_version_dir_path.tar.gz $last_version_dir_name/ >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    rm -rf $last_version_dir_path/ >/dev/null 2>&1
    mv $install_dir/$exec_name $dist_dir/ >/dev/null 2>&1
  else
    rm -f $last_version_dir_path.tar.gz >/dev/null 2>&1
    backup_error_code=$?
  fi
    
  cd - >/dev/null 2>&1
fi

echo $backup_error_code
}

clean() {
local clean_error_code=$exit_code

if [ $clean_error_code -eq 0 ]; then
  clean_error_code=$(backup_last_version)
fi
  
if [ $clean_error_code -eq 0 ]; then
  mv $exec_name $install_dir/$exec_name
  clean_error_code=$?
fi

if [ $clean_error_code -ne 0 ]; then
  rm -f  $exec_name >/dev/null 2>&1
  rm -rf $dist_dir/$package_name >/dev/null 2>&1
  if [ $new_install -eq 1 ]; then
    rm -rf $install_dir >/dev/null 2>&1
  else
    mv $dist_dir/$exec_name $install_dir/ >/dev/null 2>&1
  fi
fi

#rm -rf $package_dir >/dev/null 2>&1
}

check_version
make_install_dir
copy_package_data
create_exec_link_file
redirect_data_dir
enable_user_autostart
enable_desktop_icon
enable_dock_icon

exit $exit_code
